package com.amikomgamedev.labirinhantu.state;


import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;


public class State_Menu_Help extends State_Menu_Credits
{

	private	float[] minX	= new float[2];
	private	float[] maxX	= new float[2];
	
	private final float	DURATION	= 0.3f;
	
	private float	minCameraX;
	private float	centerCameraX;
	private	float	maxCameraX;
	
	protected void create_Menu_Help()
	{

	    minCameraX		= - RES_WIDTH;
	    centerCameraX	= - (RES_WIDTH / 2);
	    maxCameraX		= 0;
	    
	    m_Camera_Menu.initCamera(grafik.spr_Menu_Help[0]);
	    m_Camera_Menu.centerCamera();
		isCameraMove = false;
	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		if (State_Menu_Current == STATE_MENU_HELP)
		{
			m_Scroll.onTouchEvent(pSceneTouchEvent);
		}
		
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
        	if (State_Menu_Current == STATE_MENU_HELP)
        	{
        	    if (keyCode == KeyEvent.KEYCODE_BACK)
        	    {
        		grafik.spr_Menu_Help[0].setPosition(- RES_WIDTH, 0);
			grafik.spr_Menu_Help[1].setPosition(- (RES_WIDTH * 2), 0);
        
        		m_Camera_Menu.initCamera(grafik.spr_Menu_Main_Bg);
        		isCameraMove = true;
        		return super.onKeyUp(keyCode, event);
        	    }
        	}
		return super.onKeyUp(keyCode, event);
//		return false;
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		if(State_Menu_Current == STATE_MENU_HELP)
		{
				if(isCameraMove)
				{
					m_Camera_Menu.moveCamera(pSecondsElapsed);
				
					if (m_Camera_Menu.isStopCamera())
						switchState(STATE_MENU_MAIN);
				}

		}
				
		
		super.onUpdate(pSecondsElapsed);
	}	
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_MENU_HELP)
		sound.stopMenuBgm();
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_MENU_HELP)
		sound.playMenuBgm();

	    super.onResumeGame();
	}
	
	@Override
	public void onScroll(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{
        	if(State_Menu_Current == STATE_MENU_HELP)
            	{
		    for (int i = 0; i < grafik.spr_Menu_Help.length; i++)
		    {
			grafik.spr_Menu_Help[i].setPosition(
				grafik.spr_Menu_Help[i].getX() + pDistanceX, 
				grafik.spr_Menu_Help[i].getY());			
		    }
        	}
        	super.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public void onScrollFinished(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{
	    	if(State_Menu_Current == STATE_MENU_HELP)
        	{
		    for (int i = 0; i < minX.length; i++)
			minX[i]	= grafik.spr_Menu_Help[i].getX();
		    
		    for (int i = 0; i < maxX.length; i++)
			maxX[i]	= grafik.spr_Menu_Help[i].getX() + grafik.spr_Menu_Help[i].getWidth();
		    
		 // help 1 digeser ke kiri
		    if (minX[0] < minCameraX)
		    {
			float[] pToX	= {-RES_WIDTH, -(RES_WIDTH * 2)};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Help[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}
			
		    }

		    // help 2 digeser ke kanan
		    if (maxX[1] > maxCameraX)
		    {
			float[] pToX	= {0, -RES_WIDTH};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Help[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}			
		    }
		    
//		    // antara help 1 dan help 2 digeser ke help 2
		    if (maxX[1] > centerCameraX && maxX[1] < maxCameraX)
		    {
			float[] pToX	= {0, -RES_WIDTH};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Help[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}

			grafik.lst_Menu_Help_Lingkaran.get(0).setCurrentTileIndex(0);
			grafik.lst_Menu_Help_Lingkaran.get(1).setCurrentTileIndex(1);
		    }
		    
		 // antara help 1 dan help 2 digeser ke help 1
		    if (minX[0] < centerCameraX && minX[0] > minCameraX)
		    {
			float[] pToX	= {-RES_WIDTH, -(RES_WIDTH * 2)};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Help[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}

			grafik.lst_Menu_Help_Lingkaran.get(0).setCurrentTileIndex(1);
			grafik.lst_Menu_Help_Lingkaran.get(1).setCurrentTileIndex(0);
		    }
        	}
	    	
	    	super.onScrollFinished(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	
}